<?php

namespace app\agent\controller;

use think\Validate;
use think\Db;
use app\models\{Banner, Region, PlatClass, Config, PlatSons, Withdraw, Agent, Business, SystemGoodsSpec, SystemGoods, ConvenClass, Conven, AgentArea};

class System extends Base
{


    public function conven()
    {
        $list = (new Conven())->order('id desc');
        $data = $this->request->get();
        $agentarea = (new AgentArea())->where(['agentid' => $this->request->agent['id']])->select();
        $citys = [];
        $areas = [];
        $streets = [];
        foreach ($agentarea as $agareas) {
            if ($agareas['type'] == 2) {
                $citys[] = $agareas['city'];
            }
            if ($agareas['type'] == 3) {
                $areas[] = $agareas['area'];
            }
            if ($agareas['type'] == 4) {
                $streets[] = $agareas['street'];
            }
        }
        $where = [
            ['city', 'IN', $citys],
            ['type', 'IN', [2, 3, 4]],
        ];
        $where1 = [
            ['area', 'IN', $areas],
            ['type', 'IN', [3, 4]],
        ];
        $where2 = [
            ['street', 'IN', $streets],
            ['type', 'IN', [4]],
        ];
        if (!empty($data['title'])) {
            $where[] = ['title', 'like', '%' . $data['title'] . '%'];
            $where1[] = ['title', 'like', '%' . $data['title'] . '%'];
            $where2[] = ['title', 'like', '%' . $data['title'] . '%'];
        }
        if (!empty($data['classid'])) {
            $where[] = ['classid', '=', $data['classid']];
            $where1[] = ['classid', '=', $data['classid']];
            $where2[] = ['classid', '=', $data['classid']];
        }
        $list = $list->whereOr(function ($query) use ($where) {
            $query->where($where);
        })->whereOr(function ($query) use ($where1) {
            $query->where($where1);
        })->whereOr(function ($query) use ($where2) {
            $query->where($where2);
        })->paginate(10)->each(function ($row) {
            $row['city'] = array_filter(explode(',', $row['city']));
            $row['area'] = array_filter(explode(',', $row['area']));
            $row['street'] = array_filter(explode(',', $row['street']));
            $row['province_list'] = [];
            $row['city_list'] = [];
            $row['area_list'] = [];
            $row['street_list'] = [];
            $row['class_name'] = (new ConvenClass())->where('id', $row['classid'])->value('name');
            $row['province_list'] = (new Region())->where(['level' => 1, 'area_code' => $row['province']])->value('name') ?? '';
            $row['city_list'] = (new Region())->where(['level' => 2, 'area_code' => $row['city']])->value('name') ?? '';
            $row['area_list'] = (new Region())->where(['level' => 3, 'area_code' => $row['area']])->value('name') ?? '';
            $row['street_list'] = (new Region())->where(['level' => 4, 'id' => $row['street']])->value('name') ?? '';
            return $row;
        });
        $ConvenClass = (new ConvenClass())->where('isshow', 1)->order(['sort' => 'desc', 'id' => 'desc'])->select();
        return view(null, [
            'list' => $list,
            'convenclass' => $ConvenClass
        ]);
    }

    public function conven_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'title' => 'require',
                'type' => 'require|in:1,2,3,4'
            ], [
                'title.require' => '不能为空',
                'type.require' => '类型不能为空',
                'type.in' => '类型不正确',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'msg' =>  $validate->getError()]);
            }
            switch ($data['type']) {
                case '1':
                    return json(['status' => 1, 'msg' => '无区域权限']);
                    $data['province'] = $data['province'] ?? 0;
                    $data['city'] = $data['city'] ?? 0;
                    $data['area'] = $data['area'] ?? 0;
                    $data['street'] = $data['street'] ?? 0;
                    break;
                case '2':
                    if (empty($data['province'])) {
                        return json(['status' => 1, 'msg' => '请选择省份']);
                    }
                    if (empty($data['city'])) {
                        return json(['status' => 1, 'msg' => '请选择城市']);
                    }
                    $havagent = (new AgentArea())->where(['type' => 2, 'city' => $data['city'], 'agentid' => $this->request->agent['id']])->find();
                    if (empty($havagent)) {
                        return json(['status' => 1, 'msg' => '无区域权限']);
                    }
                    $data['area'] = 0;
                    $data['street'] = 0;
                    break;
                case '3':
                    if (empty($data['province'])) {
                        return json(['status' => 1, 'msg' => '请选择省份']);
                    }
                    if (empty($data['city'])) {
                        return json(['status' => 1, 'msg' => '请选择城市']);
                    }
                    if (empty($data['area'])) {
                        return json(['status' => 1, 'msg' => '请选择区域']);
                    }
                    $havagent = (new AgentArea())->where(['type' => 2, 'city' => $data['city'], 'agentid' => $this->request->agent['id']])->find();
                    if (empty($havagent)) {
                        $havagent = (new AgentArea())->where(['type' => 3, 'area' => $data['area'], 'agentid' => $this->request->agent['id']])->find();
                    }
                    if (empty($havagent)) {
                        return json(['status' => 1, 'msg' => '无区域权限']);
                    }
                    $data['street'] = 0;
                    break;
                case '4':
                    if (empty($data['province'])) {
                        return json(['status' => 1, 'msg' => '请选择省份']);
                    }
                    if (empty($data['city'])) {
                        return json(['status' => 1, 'msg' => '请选择城市']);
                    }
                    if (empty($data['area'])) {
                        return json(['status' => 1, 'msg' => '请选择区域']);
                    }
                    if (empty($data['street'])) {
                        return json(['status' => 1, 'msg' => '请选择街道']);
                    }
                    $havagent = (new AgentArea())->where(['type' => 2, 'city' => $data['city'], 'agentid' => $this->request->agent['id']])->find();
                    if (empty($havagent)) {
                        $havagent = (new AgentArea())->where(['type' => 3, 'area' => $data['area'], 'agentid' => $this->request->agent['id']])->find();
                    }
                    if (empty($havagent)) {
                        $havagent = (new AgentArea())->where(['type' => 4, 'street' => $data['street'], 'agentid' => $this->request->agent['id']])->find();
                    }
                    if (empty($havagent)) {
                        return json(['status' => 1, 'msg' => '无区域权限']);
                    }
                    break;
                default:
                    return json(['status' => 1, 'msg' => '管理员类型错误']);
                    break;
            }
            if (!empty($data['id'])) {
                $res = (new Conven())->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = (new Conven())->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['status' => 0, 'msg' => '编辑成功']);
            }
            return json(['status' => 0, 'msg' => '编辑失败']);
        } else {
            $id = $this->request->get('id/d', 0);
            $row = (new Conven())->where('id', $id)->find();
            $province = (new Region())->where(['level' => 1])->select();
            $city = [];
            $area = [];
            $street = [];
            if (!empty($row)) {
                $parent = (new Region())->where(['level' => 1, 'area_code' => $row['province']])->find();
                $city = (new Region())->where(['level' => 2, 'parent_id' => $parent['id']])->select();
                $carent = (new Region())->where(['level' => 2, 'area_code' => $row['city']])->find();
                $area = (new Region())->where(['level' => 3, 'parent_id' => $carent['id']])->select();
                $aarent = (new Region())->where(['level' => 3, 'area_code' => $row['area']])->find();
                $street = (new Region())->where(['level' => 4, 'parent_id' => $aarent['id']])->select();
            }
            $ConvenClass = (new ConvenClass())->where('isshow', 1)->order(['sort' => 'desc', 'id' => 'desc'])->select();
            return view('', [
                'row' => $row,
                'province' => $province,
                'city' => $city,
                'area' => $area,
                'street' => $street,
                'convenclass' => $ConvenClass

            ]);
        }
    }

    public function conven_del()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id/d', 0);
            if (empty($id)) {
                return json(['status' => 1, 'msg' => '请选择删除项目']);
            }
            $result = (new Conven())->where('id', $id)->find();
            if (empty($result)) {
                return json(['status' => 1, 'msg' => '项目不存在，请重试']);
            }
            switch ($result['type']) {
                case '1':
                    return json(['status' => 1, 'msg' => '无区域权限']);
                    break;
                case '2':
                    $havagent = (new AgentArea())->where(['type' => 2, 'city' => $result['city'], 'agentid' => $this->request->agent['id']])->find();
                    if (empty($havagent)) {
                        return json(['status' => 1, 'msg' => '无区域权限']);
                    }
                    break;
                case '3':
                    $havagent = (new AgentArea())->where(['type' => 2, 'city' => $result['city'], 'agentid' => $this->request->agent['id']])->find();
                    if (empty($havagent)) {
                        $havagent = (new AgentArea())->where(['type' => 3, 'area' => $result['area'], 'agentid' => $this->request->agent['id']])->find();
                    }
                    if (empty($havagent)) {
                        return json(['status' => 1, 'msg' => '无区域权限']);
                    }
                    break;
                case '4':
                    $havagent = (new AgentArea())->where(['type' => 2, 'city' => $result['city'], 'agentid' => $this->request->agent['id']])->find();
                    if (empty($havagent)) {
                        $havagent = (new AgentArea())->where(['type' => 3, 'area' => $result['area'], 'agentid' => $this->request->agent['id']])->find();
                    }
                    if (empty($havagent)) {
                        $havagent = (new AgentArea())->where(['type' => 4, 'street' => $result['street'], 'agentid' => $this->request->agent['id']])->find();
                    }
                    if (empty($havagent)) {
                        return json(['status' => 1, 'msg' => '无区域权限']);
                    }
                    break;
                default:
                    return json(['status' => 1, 'msg' => '类型错误']);
                    break;
            }
            $res = (new Conven())->where('id', $id)->delete();
            if ($res) {
                return json(['status' => 0, 'msg' => '操作成功']);
            }
            return json(['status' => 1, 'msg' => '操作失败']);
        }
    }
}
